<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      /* 轮播图 start */
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        background-color: #494f5c;
      }
      .swiper {
        position: relative;
        margin: 200px auto;
        width: 1200px;
        height: 220px;
        /* overflow: hidden; */
      }
      .swiper-ul {
        list-style: none;
        position: relative;
        margin: 0 auto;
        width: 400px;
        height: inherit;
      }
      .swiper-ul li {
        list-style: none;
        position: absolute;
        width: inherit;
        height: inherit;
        transition: all 0.6s ease-in-out;
      }
      .swiper-ul li img {
        width: 100%;
        height: 100%;
        border-radius: 10px;
      }
      .swiper-nav {
        position: absolute;
        bottom: 10px;
        left: 50%;
        transform: translateX(-50%);
        width: inherit;
        height: 40px;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        z-index: 10;
      }
      .swiper-nav > div {
        margin: 0 20px;
        width: 80px;
        height: 10px;
        background-color: #e6e6e64d;
        cursor: pointer;
        transition: all 2s ease-in-out;
      }
      .swiper-nav > div:hover {
        background-color: #e6e6e6bb;
      }
      .prev,
      .next {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
      }
      .prev {
        left: 10px;
        z-index: 10;
      }
      .next {
        right: 10px;
        z-index: 10;
      }

      /* 轮播图 end */
    </style>
  </head>
  <body>
    <!-- 结构
      div.swiper 
          img.prev
            ul.swiper-ul > li > img[src] 
          img.next
    -->

    <!-- 轮播图 start -->
    <div class="swiper">
      <img class="prev" src="./images/prev.png" alt="" />
      <ul class="swiper-ul">
        <li>
          <img src="./images/deer.jpg" alt="" />
        </li>
        <li>
          <img src="./images/deer1.jpg" alt="" />
        </li>
        <li>
          <img src="./images/deer2.jpg" alt="" />
        </li>
        <li>
          <img src="./images/deer3.jpg" alt="" />
        </li>
      </ul>
      <img class="next" src="./images/next.png" alt="" />
      <div class="swiper-nav"></div>
    </div>
    <!-- 轮播图 end -->

    <script>
      function swiper() {
        let width = document.querySelector(".swiper-ul li").clientWidth; // offsetWidth
        let liIndex = 0;
        let ul = document.querySelector(".swiper-ul"); // ul tag
        let liNodeList = document.querySelectorAll(".swiper-ul li"); // li node list
        let liList = []; // li tag list

        // 开局先启动定时器
        let timer = setInterval(function () {
          next();
        }, 4000);

        // 鼠标移入 swiper 清除定时器
        document.querySelector(".swiper").onmouseenter = function () {
          clearInterval(timer);
        };
        // 鼠标移出 swiper 开启定时器
        document.querySelector(".swiper").onmouseleave = function () {
          timer = setInterval(next, 4000);
        };

        // 把每个 li 放到 liList 数组里
        liNodeList.forEach(function (value, index) {
          liList.push(value);
        });

        let prevImg = document.querySelector(".prev"); // 上一张图的指示箭头
        let nextImg = document.querySelector(".next"); // 下一张图的指示箭头
        // 点击->上一个
        prevImg.addEventListener("click", function () {
          clearInterval(timer);
          prev();
        });

        // 点击->下一个
        nextImg.addEventListener("click", function () {
          clearInterval(timer);
          next();
        });

        // 初始化后数组的后三张图片的位置
        initImgArrPosition();

        // 上一个
        function prev() {
          liList.unshift(liList.pop());
          for (let i = 0; i < liList.length; i++) {
            liList[i].style.zIndex = i;
            liList[i].style.transform = "scale(1.0)";
          }
          initImgArrPosition();
        }

        // 下一个
        function next() {
          liList.push(liList.shift());
          for (let i = 0; i < liList.length; i++) {
            liList[i].style.zIndex = i;
            liList[i].style.transform = "scale(1.0)";
          }
          initImgArrPosition();
        }

        // 初始化后数组的后三张图片的位置
        function initImgArrPosition() {
          liList[liList.length - 4].style.left = `0`;
          liList[liList.length - 2].style.transform = "scale(0.8)";
          liList[liList.length - 4].style.zIndex = "-1";
          liList[liList.length - 3].style.left = `-${width}px`;
          liList[liList.length - 2].style.left = "0";
          liList[liList.length - 2].style.transform = "scale(1.2)";
          liList[liList.length - 2].style.zIndex = "9";
          liList[liList.length - 1].style.left = `${width}px`;
        }
      }
      swiper();
    </script>
  </body>
</html>
